package com.share.service.oa.org.impl;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.share.bean.oa.org.EmpAccount;
import com.share.bean.oa.org.EmpDimission;
import com.share.bean.oa.org.Employee;
import com.share.commons.base.query.BaseQuery;
import com.share.commons.base.query.PageModel;
import com.share.commons.util.MD5;
import com.share.dao.oa.org.EmployeeDao;
import com.share.service.oa.org.EmpDimissionManager;
import com.share.service.oa.org.EmployeeManager;
import com.share.web.formbean.oa.hr.EmployeeForm;

@Service
public class EmployeeManagerImpl implements EmployeeManager {

	@Resource
	private EmployeeDao empDao ;
	
	@Resource
	private EmpDimissionManager edm ;
	
	@Override
	public void saveEmpManager(Employee emp) throws Exception {
		//MD5加密
		EmpAccount empAccount = emp.getEmpAccount() ;
		emp.setEmpAccount(empAccount) ;
		empAccount.setPassword(MD5.MD5Encode(emp.getEmpAccount().getPassword())) ;
		
		emp.setEmpCreDate(new Date()) ;
		
		this.empDao.saveEmpDao(emp) ;
	}

	@Override
	public PageModel<Employee> managerEmpManager(EmployeeForm empform,int firstResult, int maxResult)
			throws Exception {
		String hql = "select o from Employee o" ;
		
		StringBuffer whereHQL = new StringBuffer() ;
		List<Object> param = new ArrayList<Object>() ;
		if(null != empform) {
			if(null != empform.getStartDate() && null != empform.getEndDate()) {
				whereHQL.append("o.empCreDate>=?").append(BaseQuery.AND).append("o.empCreDate<=?") ;
				param.add(empform.getStartDate()) ;
				param.add(empform.getEndDate()) ;
			} else if(null != empform.getKeyword() && !"".equals(empform.getKeyword())) {
				whereHQL.append("o.empRealname").append(BaseQuery.LIKE).append("?") ;
				param.add("%"+empform.getKeyword()+"%") ;
			}
		}
		
		LinkedHashMap<String,String> orderby = new LinkedHashMap<String, String>() ;
		
		return this.empDao.getScrollDataEmpDao(hql, whereHQL.toString(), param, firstResult, maxResult, orderby) ;
	}
	

	/** 标示员工离职 */
	@Override
	@Transactional(propagation=Propagation.REQUIRED)
	public boolean updateByParamEmpManager(Employee emp) throws Exception {
		
		String hql = "update Employee o" ;
		StringBuffer setHQL = new StringBuffer() ;
		StringBuffer whereHQL = new StringBuffer() ;
		
		setHQL.append("o.visible=?").append(",").append("dimis_id=?") ;
		whereHQL.append("o.empNo=?") ;
		try {
			EmpDimission ed = new EmpDimission() ;
			ed.setEmployee(emp) ;
			ed.setDimissionDate(emp.getDimissionMsg().getDimissionDate()) ;
			ed.setDimissionCause(emp.getDimissionMsg().getDimissionCause()) ;
			
			Serializable id = this.edm.saveDimissionManager(ed) ;
			
			this.empDao.updateByParamEmpDao(hql, setHQL.toString(), whereHQL.toString(), new Object[]{false,(Integer)id,emp.getEmpNo()});
			
			return true ;
		} catch (Exception e) {
			throw e ;
		}
	}

	@Override
	public void updateEmpManager(Employee emp) throws Exception {
		emp.setEmpCreDate(new Date()) ;
		this.empDao.updateEmpDao(emp) ;
	}

	@Override
	public Employee loadByEmpNoManager(String empNo) throws RuntimeException {
		return this.empDao.loadByEmpNoDao(empNo) ;
	}

}
